<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title></title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"/>

</head>
<body>
<div id="app">
    <h1>考试管理</h1>
    <div>
        <el-dialog :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm2" status-icon  ref="ruleForm2" label-width="100px"
                     class="demo-ruleForm">
                <el-form-item label="所属课程" prop="courseId">
                    <el-select v-model="ruleForm2.courseId" placeholder="请选择">
                        <el-option v-for="item in courses" :key="item.courseId" :label="item.courseName"
                                   :value="item.courseId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="考试时间" prop="examTime">
                    <el-date-picker v-model="ruleForm2.examTime" type="date" placeholder="选择日期"
                                    value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="发布时间" prop="examAddtime">
                    <el-date-picker v-model="ruleForm2.examAddtime" type="date" placeholder="选择日期"
                                    value-format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="考试地点" prop="examPlace">
                    <el-input v-model="ruleForm2.examPlace"></el-input>
                </el-form-item>
                <el-form-item label="考试形式" prop="examForm">
                    <el-input v-model="ruleForm2.examForm"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm()">发布</el-button>
                    <el-button @click="dialogFormVisible=false">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

        <div class="publish" style="padding-right:1700px">
            <el-button type="primary" @click="dialogFormVisible = true">发布考试信息</el-button>
        </div>

        <div class="table">
            <el-table :data="tableData" style="width: 100%">
                <el-table-column type="selection" width="50">
                </el-table-column>
                <el-table-column prop="examId" label="序号" width="120">
                </el-table-column>
                <el-table-column prop="examTime" label="考试时间" width="200" :formatter="dateFormat">
                </el-table-column>
                <el-table-column prop="course.courseName" label="课程名称" width="120">
                </el-table-column>
                <el-table-column prop="examPlace" label="考试地点" width="200">
                </el-table-column>
                <el-table-column prop="examForm" label="考试形式" width="200">
                </el-table-column>

                <el-table-column prop="examAddtime" label="发布时间" width="200" :formatter="dateFormat">
                </el-table-column>
                <el-table-column label="操作" width="220">
                    <template slot-scope="scope">
                        <el-button type="danger" icon="el-icon-delete" @click="open2(scope.row.examId)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!--分页-->
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page="pageIndex" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </div>
    </div>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>

    let _this = new Vue({
        el: '#app',
        data: {
            pageIndex: 1,
            pageSize: 10,
            total: 0,

            dialogFormVisible: false,
            tableData: [],
            courses: [],
            ruleForm2: {

                courseId: 80,
                teacherId: 1,
                examPlace: '',
                examTime: '',
                examAppointTime: '',
                examForm: '',
                examAddtime: '',
                examMotifyPerson: '0',
                examMotifyDate: '',
                examDelete: 1

            }
        },
        created: function () {
            this.getCourserList();
            this.examList2();
            let user = localStorage.getItem('teacher-user');
            if (user) {
                user = JSON.parse(user);
                this.ruleForm2.teacherId = user.teacherId

            }
        },
        methods: {
            getCourserList() {
                $.ajax({
                    type: 'post',
                    url: '/api/teacher/courseware/getCourseList',
                    data: {},
                    dataType: 'json',
                    success: function (res) {
                        // console.log(res)
                        _this.courses = res.resData
                    }
                })
            },
            /*日期格式化*/
            dateFormat: function (row, column) {
                let val = row[column.property];
                //console.log(date);
                let date = new Date(val);
                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            },
            handleSizeChange(val) {
            },
            handleCurrentChange(val) {
                this.pageIndex = val;
                this.examList2();

            },
            open2(examId) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    type: 'warning',
                    center: true
                }).then(() => {
                    $.ajax({
                        type: 'post',
                        url: '/api/teacher/exam/delete',
                        data: {id: examId},
                        dataType: 'json',
                        success: function (res) {
                            if (res.status == 200) {

                                _this.$message({
                                    message: res.message,
                                    type: 'success'
                                });
                                _this.examList2();
                            } else {
                                _this.$message({
                                    message: res.message,
                                    type: 'info'
                                })
                            }
                            ;
                        },
                        error: function (error) {
                            console.log('接口不通' + error);
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '取消删除'
                    });
                });

            },
            submitForm() {
                console.log(this.ruleForm2)
                $.ajax({
                    type: 'post',
                    url: '/api/teacher/exam/insert',
                    data:JSON.stringify(this.ruleForm2),
                    contentType : 'application/json',
                    dataType: 'json',
                    success: function (res) {
                        if (res.status == 200) {
                            _this.$message({
                                message: res.message,
                                type: 'success'
                            });
                            _this.ruleForm2.examPlace = '';
                            _this.ruleForm2.examTime = '';
                            _this.ruleForm2.examForm = '';
                            _this.ruleForm2.examAddtime = '';
                            _this.dialogFormVisible = false;
                            _this.examList2();
                        } else {
                            _this.$message({
                                message: res.message,
                                type: 'info'
                            })
                        }
                        ;
                    },
                    error: function (error) {
                        console.log('接口不通' + error);
                    }
                })

            },

            /*显示考试列表*/
            examList2() {
                $.ajax({
                    type: 'post',
                    url: '/api/teacher/exam/getlist',
                    data: {pageIndex: this.pageIndex},
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (res) {
                        _this.tableData = res.resData.list;
                        _this.total = res.resData.total
                    },
                    error: function (error) {
                        console.log('接口不通' + error);
                    }
                })
            }


        }
    })

</script>
</body>
</html>